package com.ssrs.platform.controller.handler;

import cn.hutool.core.convert.Convert;
import com.ssrs.framework.security.annotation.Priv;
import com.ssrs.framework.web.ApiResponses;
import com.ssrs.framework.web.BaseController;
import com.ssrs.platform.util.FileUploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * @author ssrs
 */
@RestController
public class CommonHandler extends BaseController {

    /**
     * 通用上传接口
     *
     * @param file
     * @return
     */
    @Priv(login = false)
    @ApiIgnore
    @PostMapping(value = "/common/upload", consumes = "multipart/form-data")
    public ApiResponses<Map<String, Object>> commonUpload(@RequestParam("file") MultipartFile file) {
        Map<String, Object> resultMap = FileUploadUtil.upload(file);
        String flag = "Flag";
        if (!Convert.toBool(resultMap.get(flag))) {
            return failure(Convert.toStr(resultMap.get("Message")));
        }
        String previewPath = Convert.toStr(resultMap.get("PreviewPath"));
        resultMap.put("url", previewPath);
        return success(resultMap);
    }
}
